package com.wpf.system.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpf.system.common.response.ResultUtils;
import com.wpf.system.common.response.ResultVO;
import com.wpf.system.entity.Field;
import com.wpf.system.entity.vo.FieldVO;
import com.wpf.system.service.FieldService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/field")
public class FieldController {
    @Resource
    FieldService fieldService;

    @GetMapping("/list/{uid}")
    public List<Field> getFieldList(@PathVariable("uid") Long uid){ return fieldService.getFieldList(uid); }

    @GetMapping("/{id}")
    public Field getFieldById(@PathVariable("id") Long id){ return fieldService.getById(id); }

    @PreAuthorize("hasAuthority('field:fieldinfo:add')")
    @PostMapping
    public ResultVO addField(@RequestBody Field field){
        field.setFieldNo("field"+IdUtil.objectId());
        if (fieldService.save(field)){
            return ResultUtils.success("添加场馆成功！",field.getFieldNo());
        }else{
            return ResultUtils.error("添加场馆失败！");
        }
    }

    @PreAuthorize("hasAuthority('field:fieldinfo:edit')")
    @PutMapping
    public ResultVO editField(@RequestBody Field field){
        boolean flag1 = true;
        if(field.getPid()==0){
            flag1 = fieldService.updateWeekBatchById(field.getWeek(),field.getId());
            if(ObjectUtil.isNotNull(field.getCid())){
                List<Field> fieldList = fieldService.list(new LambdaQueryWrapper<Field>().eq(Field::getPid,field.getId()));
                for(Field item : fieldList){
                    item.setCid(field.getCid());
                }
                fieldService.updateBatchById(fieldList);
            }
        }
        boolean flag2 = fieldService.updateById(field);
        if (flag1&&flag2){
            return ResultUtils.success("修改场馆信息成功！");
        }else{
            return ResultUtils.error("修改场馆信息失败！");
        }
    }

    @PreAuthorize("hasAuthority('field:fieldinfo:edituser')")
    @PutMapping("/user")
    public ResultVO editFieldUser(@RequestBody Field field){
        if (fieldService.editFieldUser(field.getUid(),field.getId())){
            return ResultUtils.success("修改场馆负责人成功！");
        }else{
            return ResultUtils.error("修改场馆负责人失败！");
        }
    }

    @PreAuthorize("hasAuthority('field:fieldinfo:delete')")
    @DeleteMapping("/{ids}")
    public ResultVO deleteField(@PathVariable("ids") String ids){
        List<Long> deleteIds = Convert.toList(Long.class,ids.split(","));
        if (fieldService.removeBatchByIds(deleteIds)){
            return ResultUtils.success("删除场馆成功！");
        }else{
            return ResultUtils.error("删除场馆失败！");
        }
    }

    //////////////////场馆预约部分//////////////////
    @GetMapping("/list")
    public List<FieldVO> getValidField(@RequestParam(value = "pid",required = false) Long pid){ return fieldService.getValidField(pid); }

}
